import commonConstants from "@/common/commonConstants";
import TroopManager from "./toorManager";
import { defined, ScreenSpaceEventHandler, ScreenSpaceEventType } from "cesium";
import commonMethod from "@/common/commonMethod";
import RightMenu from "@/common/rightMenu";

class TroopListecner{
    constructor(v){
        this.curViewer = v;      
        this.scene = v.scene;
        this.clock = v.clock;
        this.canvas = v.scene.canvas;
        this.camera = v.scene.camera;
        this.ellipsoid = v.scene.globe.ellipsoid;
       
        this.pickEntity = undefined;
        this.drawHandler = null;
        this.params = undefined; 
        this.isActive = false;
        this.isEdit = false;
        this.monitorKey = commonConstants.monitorKey.troop;
        
        this.rightmenu = undefined;
        this.isOneTargger = false;//这个是判断修改时的问题，他有一个初次触发，不需要事件
    
        this.callbackFunction = undefined;
        this.troopManager = undefined;
        this.monitorManager = undefined

    }

    setManager(manager){
        this.monitorManager = manager;
    }

    setActive(bool){
        let _this = this;
        _this.isActive = bool;
    }
    getViewer(){
        let _this = this;
        return _this.curViewer;
    }
    /**
     * @param troopManager {TroopManager}
     */
    setTroopManager(troopManager){
        let _this =this;
        _this.troopManager = troopManager;
    }
    setParams(params){
        let _this = this;
        _this.params = params;
    }
    setCallbackFunction(cf){
        this.callbackFunction = cf
    }
    /**
     * 这是开始标绘兵力
     */
    deploy() {
        const _this = this;
        if (!_this.drawHandler) {
            _this.drawHandler = new ScreenSpaceEventHandler(_this.canvas);

            _this.drawHandler.setInputAction(function (event) {

                if (!_this.isActive) {
                    return;
                }

                if (_this.isEdit) {
                    if (_this.isOneTargger) {
                        _this.isOneTargger = false;
                    } else {
                        //停止修改
                        _this.isEdit = false;
                        _this.monitorManager.deployDefaultMonitor();
                    }
                    return;
                }
                let p = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!p) {
                    return;
                }

                _this.destoryRightMenu();
                let cartesian = _this.scene.globe.pick(p.ray, _this.scene);

                if (!defined(cartesian)) {
                    return;
                }
                let entity = commonMethod.judgeClickEntity(_this.curViewer, p.pos);
                if (entity) {
                    //存在点击到了已经画好的标绘上，
                    if (!_this.isEdit) {
                        _this.isEdit = true;
                        _this.pickEntity = entity;
                    }
                    return;
                }

                let marker = _this.createTroop(cartesian);
                if (_this.callbackFunction) {
                    _this.callbackFunction(marker);
                }

            }, ScreenSpaceEventType.LEFT_CLICK);

            _this.drawHandler.setInputAction(function (event) {

                if (!_this.isActive) {
                    return;
                }
                _this.destoryRightMenu();

                if (!_this.pickEntity) {
                    return;
                }

                let judgeClickPosition = commonMethod.judgeClickPosition(event.endPosition,_this.curViewer);
                let cartesian = _this.scene.globe.pick(judgeClickPosition.ray, _this.scene);

                if (!defined(cartesian)) {
                    return;
                }
                _this.pickEntity.position.setValue(cartesian);
                let troop = _this.troopManager.getTroopById(_this.pickEntity.id);
                troop.setStaticPosition(cartesian);

            }, ScreenSpaceEventType.MOUSE_MOVE);

            _this.drawHandler.setInputAction(function (event) {

                if (!_this.isActive) {
                    return;
                }

                let judgeClickPosition = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!judgeClickPosition) {
                    _this.showRightMenuExit(event);
                    return;
                }

                let entity = commonMethod.judgeClickEntity(_this.curViewer, judgeClickPosition.pos);
                if (entity) {
                    _this.showRightMenuEntity(event, entity);
                } else {
                    _this.showRightMenuExit(event);
                }

                if (_this.isEdit) {
                    _this.isEdit = false;
                    _this.monitorManager.deployDefaultMonitor();
                }

            }, ScreenSpaceEventType.RIGHT_CLICK);
        }
    }
    /**
     * 生成或部署兵力,只能有两种部署，一个是单一的部署一个兵力，一个是批量部署多个兵力
     * @param cartesian 位置
     */
    createTroop(cartesian){
        let _this = this;
        let t = null;
        if (_this.params["troop"]){
            let troop = _this.params["troop"];
            _this.troopManager.deployTroop(cartesian,troop,_this.curViewer);
            t = troop;
        }else if (_this.params["troopGroup"]){
            let troopGroup = _this.params["troopGroup"];
            // let num = _this.params["depolyNum"];
            _this.troopManager.deployTroopGroup(cartesian,troopGroup,_this.curViewer);
            t = troopGroup;
        }
        return t;
    }
    destoryRightMenu(){
        let _this = this;
        if (_this.rightmenu){
            _this.rightmenu.destroy();
            _this.rightmenu = null;
            _this.monitorManager.getDefaultMonitor().setCurRigthMenu(null);
        }
    }
    /**
     * 原本是弹出推出框，现在改成直接推出标绘
     * @param event
     */
    showRightMenuExit(event){
        let _this = this;
        _this.destoryRightMenu();
        _this.monitorManager.deployDefaultMonitor();
    }
    showRightMenuEntity (event,entity) {
        let _this = this;
        _this.destoryRightMenu();
        const rightMenuOpts =[];
        //删除
        rightMenuOpts.push({
            itemName: "删除",
            icon: "layui-icon layui-icon-delete",
            callback: function () {
                //这里的删除应该是取消部署
                let troop = _this.troopManager.getTroopById(entity.id);
                troop.setShow(false);
                troop.setDeploy(false);
            }
        })
        _this.rightmenu = new RightMenu(rightMenuOpts,_this.curViewer);
        _this.rightmenu.show(event,true);
        _this.monitorManager.getDefaultMonitor().setCurRigthMenu(_this.rightmenu);
    }
    /**
     * 处理默认监听带来的事件
     */
    handerEvent(event,pickEntity,clickType) {
        let _this = this;
        _this.pickEntity = pickEntity;

        if (clickType === ScreenSpaceEventType.RIGHT_CLICK){
            //右键菜单
            _this.showRightMenuEntity(event,pickEntity);
        }else if (clickType === ScreenSpaceEventType.LEFT_CLICK){
            _this.isEdit = true;
            _this.isActive = true;
            _this.isOneTargger = true;
        }
    }
}

export default TroopListecner;